package svgedit.gui.actions;

import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.AbstractAction;

import svgedit.gui.FileChooserDialog;
import svgedit.gui.Frame;
import svgedit.gui.SupportedFiles;

/**
 * Shows discard changes dialog, then presents a file dialog for the user to
 * select an existing document to edit.
 */
@SuppressWarnings("serial")
public class OpenDocumentAction
	extends AbstractAction
{

    private Frame frame;

    /** Create this action for the given frame */
    public OpenDocumentAction(Frame frame)
    {
	super("Open...");
	this.frame = frame;
    }

    @Override
    public void actionPerformed(ActionEvent ae)
    {
	if (frame.confirmSaveChanges())
	{
	    FileChooserDialog dialog =
		    new FileChooserDialog(FileChooserDialog.FILE_OPEN_DIALOG,
			    frame.getPreferences().getDefaultPath());

	    if (!(dialog.showOpenDialog(frame) == 
		FileChooserDialog.APPROVE_OPTION))
		return;

	    File file = dialog.getSelectedFile();
	    if (file != null
		    && SupportedFiles.FileFilters.SVG_FILES_FILTER
		    .validExt(file))
	    {
		frame.getPreferences().setDefaultPath(file.getPath());
		frame.openFile(file);
	    }
	}
    }

}
